package com.chenglu.android.cart.adapter;



import com.chenglu.android.cart.CartFragment;
import com.chenglu.android.cart.bean.CartBean;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.MoneyUtils;
import com.chenglu.android.logistics.bean.LogisticsData;
import com.chenglu.android.payment.bean.OrderGoodsList;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023/3/29 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：购物车管理数据类
 */
public class CartManager {
    public String cartId;
    public int num;

    public   String tenId;
//    public String tenantId;
    public int shopTypeId;
    public int goodsAmount;
    public int isSpec;
    public int status;
    public String goodsSkuId;

    private final CartFragment fragment;
    private List<CartBean.Data> dataList=new ArrayList<>();

    public CartManager(CartFragment cartFragment) {
        fragment = cartFragment;
    }


    public List<CartBean.Data> getList() {
        return dataList;
    }

    public void setList(List<CartBean.Data> list) {
        dataList.clear();
        dataList = list;
    }


    public void setCartNum() {
        for (int i = 0; i < dataList.size(); i++) {
            for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                if (cartId.equals(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId())) {
                    dataList.get(i).getShoppingTrolleyVOList().get(j).setGoodsAmount(num);
                    return;
                }
            }
        }

    }


//    public void  setCartChenk2(String id){
//
//        for (int i = 0; i < ; i++) {
//
//        }
//    }

//     如果点击商品勾选， 就对商品进行勾选或者不勾选处理，
//     查一下这个商品所属店铺的其他商品，如果都勾选，就把商铺都勾选，反过来也一样。然后计算数量和价格

    public void setCartCheck(String id) {
        for (int i = 0; i < dataList.size(); i++) {
            if (id.equals(dataList.get(i).getTenantId())) {
                dataList.get(i).setSellerCheck(!dataList.get(i).isSellerCheck());


                for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
//                    if(id.equals( dataList.get(i).getShoppingTrolleyVOList().get(j).getTenantId())){
                        dataList.get(i).getShoppingTrolleyVOList().get(j).setSellerItemCheck(dataList.get(i).isSellerCheck());
//                    }
                }
                break;
            }


//            for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
//                if (id.equals(dataList.get(i).getShoppingTrolleyVOList().get(j).getTenantId())) {
//                    dataList.get(i).getShoppingTrolleyVOList().get(j).setSellerItemCheck(!dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck());
//                    checkShopSelect(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId());
//                    break;
//                }
//            }

        }
        getTotalMoneyte(id);
//        checkAllSelect(id);
    }

    public void  setCartCheck2(String tenantId,String goodsId){
        for (int i = 0; i <dataList.size() ; i++) {
            if(dataList.get(i).getTenantId().equals(tenantId)){

                for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                    if (goodsId.equals(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId())) {
                        dataList.get(i).getShoppingTrolleyVOList().get(j).setSellerItemCheck(!dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck());
//
                        checkShopSelect(tenantId);

                        break;
                    }

                }
            }
        }
        getTotalMoneyte(tenantId);
//        checkAllSelect1(goodsId);

    }

//检查 店铺是否全选
    private void checkShopSelect(String tenantId) {
        for (int i = 0; i < dataList.size(); i++) {
            if(tenantId.equals(dataList.get(i).getTenantId())){

                for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                    //判断是不是这个店铺的商品 如果都选中了  店铺就全选
//                    if (goodsId.equals(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId())) {
                        if (dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck()) {
                            dataList.get(i).setSellerCheck(true);
                        } else {
                            dataList.get(i).setSellerCheck(false);
                            return;

                        }
//                    }
                }
            }

        }

    }

    private void checkAllSelect(String tenantId) {
        for (int i = 0; i < dataList.size(); i++) {

            if (dataList.get(i).isSellerCheck()) {
                fragment.setAllSelect(true);
            } else {
                fragment.setAllSelect(false);
                break;
            }
        }
        getTotalMoneyte(tenantId);
    }



//    public void checkAll(boolean checked) {
//        for (int i = 0; i < dataList.size(); i++) {
//            dataList.get(i).setSellerCheck(checked);
//            for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
//                dataList.get(i).getShoppingTrolleyVOList().get(j).setSellerItemCheck(checked);
//            }
//        }
//        getTotalMoney();
//    }

    public String getTotalMoney(String tenantId,String goodsId) {
        String sum = "0.00";
        int totalAmount=0;
        LogUtils.d("getTotalMoney="+sum);
        for (int i = 0; i < dataList.size(); i++) {
            if (dataList.get(i).getTenantId().equals(tenantId)){
                for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                    LogUtils.d("getTotalMoney=isSellerItemCheck："+dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck());
                    if (dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck()){
                        LogUtils.d("getTotalMoney=getGoodsPrice："+dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsPrice());
                        LogUtils.d("getTotalMoney=getGoodsAmount："+dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount());
                        if(goodsId.equals(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId())){
                            sum = MoneyUtils.Algorithm.add(sum, MoneyUtils.Algorithm.multiply(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsPrice() + "", dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount() + "", 2));
                            totalAmount +=dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount();
                        }

                    }

                }
                LogUtils.d("算得值="+sum);
                LogUtils.d("已选择="+totalAmount);
                dataList.get(i).setTotalPrice(sum);
                dataList.get(i).setNumber(totalAmount);
            }

        }
        return  sum;
    }


    public String getTotalMoneyte(String tenantId) {
        String sum = "0.00";
        int totalAmount=0;
        LogUtils.d("getTotalMoneyte="+sum);
        for (int i = 0; i < dataList.size(); i++) {
            if (dataList.get(i).getTenantId().equals(tenantId)){
                for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                    LogUtils.d("getTotalMoneyte=isSellerItemCheck："+dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck());
                    if (dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck()){
                        LogUtils.d("getTotalMoneyte=getGoodsPrice："+dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsPrice());
                        LogUtils.d("getTotalMoneyte=getGoodsAmount："+dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount());

                        sum = MoneyUtils.Algorithm.add(sum, MoneyUtils.Algorithm.multiply(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsPrice() + "", dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount() + "", 2));
                        totalAmount +=dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount();
                    }

                }
                LogUtils.d("算得值="+sum);
                LogUtils.d("已选择="+totalAmount);
                dataList.get(i).setTotalPrice(sum);
                dataList.get(i).setNumber(totalAmount);
            }

//            for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
//                if (dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck()
//                 && dataList.get(i).getShoppingTrolleyVOList().get(j).getTenantId().equals(tenantId)) {
//                    sum = MoneyUtils.Algorithm.add(sum, MoneyUtils.Algorithm.multiply(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsPrice() + "", dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsAmount() + "", 2));
//                }
//            }
        }
//        LogUtils.d("算得值="+sum);
//        fragment.setSum(sum);
        return  sum;
    }
    public String getCartChecked() {
        String ids="";
        for (int i = 0; i < dataList.size(); i++) {
            for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                if (dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck()) {
                    ids+=ids+dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId()+",";
                }
            }
        }
        return ids;
    }

    public ArrayList<OrderGoodsList.OrderGoodsListData> getOrderConfirm(String tenantId) {

        ArrayList<OrderGoodsList.OrderGoodsListData> cartIds= new ArrayList<>();

        for (int i = 0; i < dataList.size(); i++) {
            if(tenantId.equals(dataList.get(i).getTenantId())){
                for (int j = 0; j < dataList.get(i).getShoppingTrolleyVOList().size(); j++) {
                    if (dataList.get(i).getShoppingTrolleyVOList().get(j).isSellerItemCheck()) {
                        CartBean.Data.ShoppingTrolleyVOList shoppingTrolleyVOList = dataList.get(i).getShoppingTrolleyVOList().get(j);

                        OrderGoodsList.OrderGoodsListData data =  new OrderGoodsList.OrderGoodsListData();

//                        cartIds.add(dataList.get(i).getShoppingTrolleyVOList().get(j).getGoodsId());

                        data.setCategoryIds(shoppingTrolleyVOList.getCategoryIds());//分类ids
                        data.setGoodsId(shoppingTrolleyVOList.getGoodsId());//商品id
                        data.setGoodsName(shoppingTrolleyVOList.getGoodsName());//商品名称
                        data.setIsSpec(shoppingTrolleyVOList.getIsSpec());//类别区分
//        orderGoodsListData.setGoodsSkuId(goodsInfoBean.getData().getGoodsSkus().get(0).getId());//类别区分
                        data.setDiscountPrice(dataList.get(i).getDiscountAmount());//打折价格
//                        data.setPrice(shoppingTrolleyVOList.getGoodsPrice());
                        //设置数量
                        data.setAmount(shoppingTrolleyVOList.getGoodsAmount());
                        if (shoppingTrolleyVOList.getGoodsSkuId().equals("-1")) {
                            data.setGoodsSkuId("");
                        } else {
                            data.setGoodsSkuId(shoppingTrolleyVOList.getGoodsSkuId());
                        }
                        cartIds.add(data);
                    }
                }
            }

        }
        LogUtils.e("cartids="+ cartIds);
        return cartIds;
    }

}
